package ru.electron.menu.fragments;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.opengl.Matrix;
import android.os.Handler;
import android.provider.BaseColumns;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import ru.electron.menu.R;
import ru.electron.menu.provider.MenuProvider;

import javax.xml.soap.Text;

import static ru.electron.menu.provider.MenuProvider.*;

public class OrderListFragment extends DishListFragment {

    private static final String KEY_MATRIX = "matrix";

    private class OrderAdapter extends CursorAdapter {

        public OrderAdapter(Cursor c) {
            //TODO: if I would have time I check new api

            super(getActivity(), c, CursorAdapter.FLAG_AUTO_REQUERY);
        }


        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            cursor.moveToPosition(position);
            if (cursor.getColumnIndex(KEY_MATRIX) != -1) {
                return false;
            }
            return true;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            cursor.moveToPosition(position);
            if (cursor.getColumnIndex(KEY_MATRIX) != -1) {
                return 0;
            }
            return 1;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            cursor.moveToPosition(position);
            if (cursor.getColumnIndex(KEY_MATRIX) != -1) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.title_item, parent, false);
                }

                TextView textView = (TextView) convertView.findViewById(R.id.title_elem);
                textView.setText(cursor.getString(cursor.getColumnIndex(KEY_MATRIX)));
                return convertView;
            }
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.order_list_item, parent, false);
            }
            //   System.out.println("a " + cursor.getColumnCount());
            TextView priceView = (TextView) convertView.findViewById(R.id.price);
            TextView countView = (TextView) convertView.findViewById(R.id.count);
            TextView textView = (TextView) convertView.findViewById(R.id.dish_name);
            textView.setText(cursor.getString(cursor.getColumnIndex(KEY_DISH_NAME)));
            int id = cursor.getColumnIndex(MenuProvider.KEY_COUNT);
            if (id == -1) {
                id = cursor.getColumnIndex(KEY_ORDERED);
            }
            countView.setText(cursor.getInt(id) + "");
            double total = cursor.getDouble(cursor.getColumnIndex(MenuProvider.KEY_PRICE)) * cursor.getInt(id);

            priceView.setText(total + "");
            return convertView;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
            return null;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

        }


    }


    public Cursor getCursor() {
        return cursor;
    }

    @Override
    public String getHeader() {
        return getString(R.string.order_list_header);
    }

    @Override
    public ListAdapter getListAdapter() {
        MatrixCursor c0 = new MatrixCursor(new String[]{BaseColumns._ID, KEY_MATRIX});
        Cursor c1 = getActivity().managedQuery(MenuProvider.URI_ORDER, null, null, null, null);

        Cursor c2 = getActivity().managedQuery(MenuProvider.URI_SENT, new String[]{BaseColumns._ID, KEY_ORDERED, KEY_DISH_NAME, KEY_PRICE, KEY_DESCRIPTION, KEY_URI},
                null, null, null);
        MatrixCursor c3 = new MatrixCursor(new String[]{BaseColumns._ID, KEY_MATRIX});
        if (c1.getCount() != 0) {
            c0.addRow(new Object[]{0, getString(R.string.newly)});
        }
        if (c2.getCount() != 0) {
            c3.addRow(new Object[]{10, getString(R.string.already_ordered)});
        }
        cursor = new MergeCursor(new Cursor[]{c0, c1, c3, c2});
        cursor.setNotificationUri(getActivity().getContentResolver(), CONTENT_URI);
        return new OrderAdapter(cursor);
    }
}
